package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunSleepBindMapper;
import com.csun.cmny.provider.device.csun.sleep.entity.CsunSleepData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.csunsleep.BindCsunSleepDto;
import com.csun.cmny.provider.model.dto.csunsleep.CsunSleepPageQuery;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectDeviceDataDto;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectSleepDataDto;
import com.csun.cmny.provider.model.dto.sleep.BedOldmanSleepReqDto;
import com.csun.cmny.provider.model.enums.csunsleep.DmcCsunSleepBindTypeEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.CsunSleepDataReqVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.csunsleep.CsunSleepVo;
import com.csun.cmny.provider.model.vo.csunsleep.DmcCsunSleepVo;
import com.csun.cmny.provider.model.vo.life.DmcDeviceByRoom;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.model.vo.propertyManagement.SleepDataVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-13 15:51
 * @Description: the class dmc csun sleep bind service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcCsunSleepBindServiceImpl extends BaseService<DmcCsunSleepBind>
        implements DmcCsunSleepBindService {

    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunSleepBindMapper dmcCsunSleepBindMapper;
    @Resource
    private DmcDeviceAlertService dmcDeviceAlertService;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private DmcCsunSleepDataService dmcCsunSleepDataService;
    @Resource
    private DmcCsunSleepDataHiService dmcCsunSleepDataHiService;
    @Resource
    private DmcCsunSleepDataInBedService dmcCsunSleepDataInBedService;
    @Resource
    private DmcAppSleepDataRecordService dmcAppSleepDataRecordService;
    @Resource
    private DmcAppSleepRangeRecordService dmcAppSleepRangeRecordService;
    @Resource
    private DmcCsunSleepThresholdService dmcCsunSleepThresholdService;
    @Resource
    private DmcCsunSleepConfigService dmcCsunSleepConfigService;
    @Resource
    private DmcLifeTransducerRoomConfigService dmcLifeTransducerRoomConfigService;

    /**
     * query group csun sleep list with page
     *
     * @param csunSleepPageQuery csun sleep page query
     * @return the page info.
     */
    @Override
    public PageInfo queryGroupCsunSleepListWithPage(CsunSleepPageQuery csunSleepPageQuery) {

        PageHelper.startPage(csunSleepPageQuery.getPageNum(), csunSleepPageQuery.getPageSize());

        List<DmcCsunSleepVo> dmcCsunSleepVoList = dmcCsunSleepBindMapper
                .queryGroupCsunSleepListWithPage(csunSleepPageQuery);

        return new PageInfo<>(dmcCsunSleepVoList);
    }

    /**
     * bind csun sleep
     *
     * @param bindCsunSleepDto bind csun sleep dto
     * @param loginAuthDto     login auth dto
     * @return the int.
     */
    @Override
    public Integer bindCsunSleep(BindCsunSleepDto bindCsunSleepDto, LoginAuthDto loginAuthDto) {

        // 1.校验日创床垫
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunSleepByDeviceId(bindCsunSleepDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610034, bindCsunSleepDto.getDeviceId());
        }

        // 2.校验日创床垫是否已经被绑定
        DmcCsunSleepBind dmcCsunSleepBindQuery = dmcCsunSleepBindMapper
                .selectByPrimaryKey(bindCsunSleepDto.getDeviceId());
        if (PublicUtil.isNotEmpty(dmcCsunSleepBindQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610035, dmcCsunSleepBindQuery.getDeviceId(),
                    dmcCsunSleepBindQuery.getLocation());
        }

        // 3.校验绑定类型
        Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getBindType()),
                cmnyMessageSource.getMessage("csun.sleep.bind.type.null", null));

        Preconditions.checkArgument(bindCsunSleepDto.getBindType() >= 0
                        && bindCsunSleepDto.getBindType() <= 4,
                cmnyMessageSource.getMessage("csun.sleep.bind.type.range", null));

        // TODO 床垫和老人、床位绑定关系 1:1:1

        // 4.校验位置
        if (bindCsunSleepDto.getBindType().equals(DmcCsunSleepBindTypeEnum.CUSTOM.getKey())) {
            Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getLocation()),
                    cmnyMessageSource.getMessage("csun.sleep.bind.location.null", null));
        }

        if (bindCsunSleepDto.getBindType().equals(DmcCsunSleepBindTypeEnum.OLDMAN.getKey())) {
            // 校验老人Id和老人姓名
            Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getOldmanId()),
                    cmnyMessageSource.getMessage("csun.sleep.bind.oldman.id.null", null));

            Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getOldmanName()),
                    cmnyMessageSource.getMessage("csun.sleep.bind.oldman.name.null", null));
            // TODO 校验老人Id是否存在
            OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = omcRpcService
                    .queryOldmanBedRoomBuildingByOldmanId(bindCsunSleepDto.getOldmanId());
            if (PublicUtil.isEmpty(oldmanBedRoomBuildingVo)) {
                // 老人不存在
                throw new DmcBizException(ErrorCodeEnum.DMC100610062);
            }

            /**
             * @date 2019-05-22
             * @author chisj
             * @desc 校验老人是否绑定过日创床垫
             */
            dmcCsunSleepBindQuery = new DmcCsunSleepBind();
            dmcCsunSleepBindQuery.setBindType(DmcCsunSleepBindTypeEnum.OLDMAN.getKey());
            dmcCsunSleepBindQuery.setOldmanId(bindCsunSleepDto.getOldmanId());
            int count = dmcCsunSleepBindMapper.selectCount(dmcCsunSleepBindQuery);
            if (count > 0) {
                // 老人已经绑定了日创床垫
                throw new DmcBizException(ErrorCodeEnum.DMC100610066);
            }

            bindCsunSleepDto.setOldmanName(oldmanBedRoomBuildingVo.getOldmanName());
            bindCsunSleepDto.setBedId(oldmanBedRoomBuildingVo.getBedId());
            if (PublicUtil.isNotEmpty(oldmanBedRoomBuildingVo.getLocation())) {
                bindCsunSleepDto.setLocation(oldmanBedRoomBuildingVo.getLocation());
            }
        }

        // 5.校验房间Id
        if (bindCsunSleepDto.getBindType().equals(DmcCsunSleepBindTypeEnum.BED.getKey())) {
            // 绑定类型为房间
            Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getBedId()),
                    cmnyMessageSource.getMessage("csun.sleep.bind.bed.id.null", null));
            // 校验房间Id是否存在
            // TODO 校验床位Id是否存在
            OldmanBedRoomBuildingVo bedRoomBuildingVo = omcRpcService
                    .queryOldmanBedRoomBuildingByBedId(bindCsunSleepDto.getBedId());
            if (PublicUtil.isEmpty(bedRoomBuildingVo)) {
                // 床位不存在
                throw new DmcBizException(ErrorCodeEnum.DMC100610060);
            }

            /**
             * @date 2019-05-22
             * @author chisj
             * @desc 校验床位是否绑定过日创床垫
             */
            dmcCsunSleepBindQuery = new DmcCsunSleepBind();
            dmcCsunSleepBindQuery.setBindType(DmcCsunSleepBindTypeEnum.BED.getKey());
            dmcCsunSleepBindQuery.setBedId(bindCsunSleepDto.getBedId());
            int count = dmcCsunSleepBindMapper.selectCount(dmcCsunSleepBindQuery);
            if (count > 0) {
                // 床位已经绑定了日创床垫
                throw new DmcBizException(ErrorCodeEnum.DMC100610067);
            }

            bindCsunSleepDto.setOldmanId(bedRoomBuildingVo.getOldmanId());
            bindCsunSleepDto.setOldmanName(bedRoomBuildingVo.getOldmanName());
            bindCsunSleepDto.setLocation(bedRoomBuildingVo.getLocation());
        }

        /*
        * 配置阈值
        * */
        switch (bindCsunSleepDto.getBindType()) {
            case DeviceConstant.DEVICE_BIND_TYPE_CUSTOM:
                break;
            case DeviceConstant.DEVICE_BIND_TYPE_OLDMAN:
                break;
            case DeviceConstant.DEVICE_BIND_TYPE_BED:
                Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getBedId()),
                        cmnyMessageSource.getMessage("csun.sleep.bind.bed.id.null", null));

                //根据床位查询房间配置
                OldmanBedRoomBuildingVo vo = omcRpcService.queryOldmanBedRoomBuildingByBedId(bindCsunSleepDto.getBedId());

                //查询房间阈值
                DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfig = dmcLifeTransducerRoomConfigService.selectByKey(vo.getRoomId());
                if (dmcLifeTransducerRoomConfig != null) {
                    DmcCsunSleepThreshold csunSleepThreshold = new DmcCsunSleepThreshold();
                    csunSleepThreshold.setDeviceId(bindCsunSleepDto.getDeviceId());
                    csunSleepThreshold.setMinHeartbeatThreshold(dmcLifeTransducerRoomConfig.getHeartbeatMinimum());
                    csunSleepThreshold.setMaxHeartbeatThreshold(dmcLifeTransducerRoomConfig.getHeartbeatMaximum());
                    csunSleepThreshold.setMinBreathThreshold(dmcLifeTransducerRoomConfig.getBreathMinTimes());
                    csunSleepThreshold.setMaxBreathThreshold(dmcLifeTransducerRoomConfig.getBreathMaxTimes());
                    csunSleepThreshold.setLeaveBedMeasuringTimeInterval(dmcLifeTransducerRoomConfig.getLeaveBedMeasuringTimeInterval());
                    int retThreshold = dmcCsunSleepThresholdService.saveOrUpdate(csunSleepThreshold);

                    DmcCsunSleepConfig csunSleepConfig = new DmcCsunSleepConfig();
                    csunSleepConfig.setDeviceId(bindCsunSleepDto.getDeviceId());
                    csunSleepConfig.setVitalMeasuringTimeInterval(dmcLifeTransducerRoomConfig.getVitalMeasuringTimeInterval());
                    int retConfig = dmcCsunSleepConfigService.saveOrUpdate(csunSleepConfig);
                }

                break;
            case DeviceConstant.DEVICE_BIND_TYPE_ROOM:

                Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getRoomId()),
                        cmnyMessageSource.getMessage("csun.sleep.bind.room.id.null", null));

                //查询房间阈值
                DmcLifeTransducerRoomConfig roomConfig = dmcLifeTransducerRoomConfigService.selectByKey(bindCsunSleepDto.getRoomId());
                if (roomConfig != null) {
                    DmcCsunSleepThreshold csunSleepThreshold = new DmcCsunSleepThreshold();
                    csunSleepThreshold.setDeviceId(bindCsunSleepDto.getDeviceId());
                    csunSleepThreshold.setMinHeartbeatThreshold(roomConfig.getHeartbeatMinimum());
                    csunSleepThreshold.setMaxHeartbeatThreshold(roomConfig.getHeartbeatMaximum());
                    csunSleepThreshold.setMinBreathThreshold(roomConfig.getBreathMinTimes());
                    csunSleepThreshold.setMaxBreathThreshold(roomConfig.getBreathMaxTimes());
                    csunSleepThreshold.setLeaveBedMeasuringTimeInterval(roomConfig.getLeaveBedMeasuringTimeInterval());
                    int retThreshold = dmcCsunSleepThresholdService.saveOrUpdate(csunSleepThreshold);

                    DmcCsunSleepConfig csunSleepConfig = new DmcCsunSleepConfig();
                    csunSleepConfig.setDeviceId(bindCsunSleepDto.getDeviceId());
                    csunSleepConfig.setVitalMeasuringTimeInterval(roomConfig.getVitalMeasuringTimeInterval());
                    int retConfig = dmcCsunSleepConfigService.saveOrUpdate(csunSleepConfig);
                }

                break;

            case DeviceConstant.DEVICE_BIND_TYPE_LIFE:
                Preconditions.checkArgument(!PubUtils.isNull(bindCsunSleepDto.getRoomId()),
                        cmnyMessageSource.getMessage("csun.sleep.bind.room.id.null", null));

                // TODO 校验LIFE-ROOM是否存在

                /**
                 * @date 2019-05-22
                 * @author chisj
                 * @desc 校验床位是否绑定过日创床垫
                 */
                dmcCsunSleepBindQuery = new DmcCsunSleepBind();
                dmcCsunSleepBindQuery.setBindType(DmcCsunSleepBindTypeEnum.LIFE_ROOM.getKey());
                dmcCsunSleepBindQuery.setRoomId(bindCsunSleepDto.getRoomId());
                int count = dmcCsunSleepBindMapper.selectCount(dmcCsunSleepBindQuery);
                if (count > 0) {
                    // TODO 房间已经绑定了日创床垫
                    throw new DmcBizException(ErrorCodeEnum.DMC100614008);
                }

                //查询房间阈值
                DmcLifeTransducerRoomConfig dmcLifeTransducerRoomConfigLife = dmcLifeTransducerRoomConfigService.selectByKey(bindCsunSleepDto.getRoomId());
                if (dmcLifeTransducerRoomConfigLife != null) {
                    DmcCsunSleepThreshold csunSleepThreshold = new DmcCsunSleepThreshold();
                    csunSleepThreshold.setDeviceId(bindCsunSleepDto.getDeviceId());
                    csunSleepThreshold.setMinHeartbeatThreshold(dmcLifeTransducerRoomConfigLife.getHeartbeatMinimum());
                    csunSleepThreshold.setMaxHeartbeatThreshold(dmcLifeTransducerRoomConfigLife.getHeartbeatMaximum());
                    csunSleepThreshold.setMinBreathThreshold(dmcLifeTransducerRoomConfigLife.getBreathMinTimes());
                    csunSleepThreshold.setMaxBreathThreshold(dmcLifeTransducerRoomConfigLife.getBreathMaxTimes());
                    csunSleepThreshold.setLeaveBedMeasuringTimeInterval(dmcLifeTransducerRoomConfigLife.getLeaveBedMeasuringTimeInterval());
                    int retThreshold = dmcCsunSleepThresholdService.saveOrUpdate(csunSleepThreshold);

                    DmcCsunSleepConfig csunSleepConfig = new DmcCsunSleepConfig();
                    csunSleepConfig.setDeviceId(bindCsunSleepDto.getDeviceId());
                    csunSleepConfig.setVitalMeasuringTimeInterval(dmcLifeTransducerRoomConfigLife.getVitalMeasuringTimeInterval());
                    int retConfig = dmcCsunSleepConfigService.saveOrUpdate(csunSleepConfig);
                }

                break;
        }

        // 6.添加绑定
        DmcCsunSleepBind dmcCsunSleepBind = new DmcCsunSleepBind();
        BeanUtils.copyProperties(bindCsunSleepDto, dmcCsunSleepBind);
        dmcCsunSleepBind.setUpdateInfo(loginAuthDto);

        int result = dmcCsunSleepBindMapper.insertSelective(dmcCsunSleepBind);
        if (result > 0) {
            // 设置redisKey
            CsunSleepData csunSleepData = new CsunSleepData();
            csunSleepData.setDeviceId(dmcDevice.getId());
            csunSleepData.setDeviceNo(dmcDevice.getDeviceNo());
            csunSleepData.setGroupId(dmcDevice.getGroupId());

            redisTemplate.opsForValue().set(RedisKeyUtil.getCsunSleepKey(dmcDevice.getDeviceNo()),
                    csunSleepData);
            DmcCsunSleepData dmcCsunSleepData = new DmcCsunSleepData();
            BeanUtils.copyProperties(csunSleepData, dmcCsunSleepData);
            dmcCsunSleepDataService.save(dmcCsunSleepData);
        } else {
            throw new DmcBizException(ErrorCodeEnum.DMC100610071);
        }

        return result;
    }

    /**
     * break csun sleep
     *
     * @param deviceId device id
     * @return the int.
     */
    @Override
    public Integer breakCsunSleep(Long deviceId) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunSleepByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610034, deviceId);
        }

        // 2.判断智能床垫-日创是否已经绑定
        DmcCsunSleepBind dmcCsunSleepBindQuery = dmcCsunSleepBindMapper
                .selectByPrimaryKey(deviceId);
        if (PublicUtil.isEmpty(dmcCsunSleepBindQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610036, deviceId);
        }

        // 3.解绑智能床垫-日创
        Integer result = dmcCsunSleepBindMapper.deleteByPrimaryKey(deviceId);
        if (result > 0) {

            if(dmcCsunSleepBindQuery.getBindType().equals(DeviceConstant.DEVICE_BIND_TYPE_LIFE)){
                dmcDeviceAlertService.deleteByDeviceId(deviceId);
            }

            // 删除设备绑定数据
            redisTemplate.delete(RedisKeyUtil.getCsunSleepKey(dmcDevice.getDeviceNo()));
            dmcCsunSleepDataService.deleteByKey(deviceId);
            DmcCsunSleepDataHi dmcCsunSleepDataHi = new DmcCsunSleepDataHi();
            dmcCsunSleepDataHi.setDeviceId(deviceId);
            dmcCsunSleepDataHiService.delete(dmcCsunSleepDataHi);

            /**
             * @date    2020-07-24
             * @author  chisj
             * @desc    解绑 删除报警数据，防止重新绑定查询出之前的脏数据
             */
            DmcDeviceAlert deviceAlert = new DmcDeviceAlert();
            deviceAlert.setDeviceId(deviceId);
            dmcDeviceAlertService.delete(deviceAlert);

            DmcCsunSleepDataInBed dmcCsunSleepDataInBed = new DmcCsunSleepDataInBed();
            dmcCsunSleepDataInBed.setDeviceId(deviceId);
            dmcCsunSleepDataInBedService.delete(dmcCsunSleepDataInBed);

            DmcAppSleepDataRecord appSleepDataRecord = new DmcAppSleepDataRecord();
            appSleepDataRecord.setDeviceId(deviceId);
            dmcAppSleepDataRecordService.delete(appSleepDataRecord);

            DmcAppSleepRangeRecord appSleepRangeRecord = new DmcAppSleepRangeRecord();
            appSleepRangeRecord.setDeviceId(deviceId);
            dmcAppSleepRangeRecordService.delete(appSleepRangeRecord);

            //删除床垫阈值,配置
            dmcCsunSleepThresholdService.deleteByKey(deviceId);
            dmcCsunSleepConfigService.deleteByKey(deviceId);

            //删除该设备的所有报警信息
            DmcDeviceAlert dmcDeviceAlert = new DmcDeviceAlert();
            dmcDeviceAlert.setDeviceId(deviceId);
            dmcDeviceAlertService.delete(dmcDeviceAlert);

        }
        return result;
    }

    /**
     * query csun sleep bind list all
     *
     * @return the list
     */
    @Override
    public List<DmcCsunSleepVo> queryCsunSleepBindListAll() {

        CsunSleepPageQuery csunSleepPageQuery = new CsunSleepPageQuery();
        csunSleepPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex());

        return dmcCsunSleepBindMapper.queryGroupCsunSleepListWithPage(csunSleepPageQuery);
    }

    /**
     * get csun sleep bind by device id
     *
     * @param deviceId device id
     * @return dmc csun sleep vo
     */
    @Override
    public DmcCsunSleepVo getCsunSleepBindByDeviceId(Long deviceId) {

        CsunSleepPageQuery csunSleepPageQuery = new CsunSleepPageQuery();
        csunSleepPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex());
        csunSleepPageQuery.setDeviceId(deviceId);

        PageHelper.startPage(1, 1);

        List<DmcCsunSleepVo> dmcCsunSleepVos = dmcCsunSleepBindMapper
                .queryGroupCsunSleepListWithPage(csunSleepPageQuery);

        if (PubUtils.isNull(dmcCsunSleepVos)) {
            return null;
        }

        return dmcCsunSleepVos.get(0);
    }

    /**
     * get csun sleep bind by device no
     *
     * @param deviceNo device no
     * @return dmc csun sleep vo
     */
    @Override
    public DmcCsunSleepVo getCsunSleepBindByDeviceNo(String deviceNo) {

        CsunSleepPageQuery csunSleepPageQuery = new CsunSleepPageQuery();
        csunSleepPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex());
        csunSleepPageQuery.setDeviceNo(deviceNo);

        PageHelper.startPage(1, 1);

        List<DmcCsunSleepVo> dmcCsunSleepVos = dmcCsunSleepBindMapper
                .queryGroupCsunSleepListWithPage(csunSleepPageQuery);

        if (PubUtils.isNull(dmcCsunSleepVos)) {
            return null;
        }

        return dmcCsunSleepVos.get(0);
    }

    /**
     * get csun sleep bind by oldman id
     *
     * @param oldmanId oldman id
     * @return dmc csun sleep vo
     */
    @Override
    public DmcCsunSleepVo getCsunSleepBindByOldmanId(Integer oldmanId) {

        CsunSleepPageQuery csunSleepPageQuery = new CsunSleepPageQuery();
        csunSleepPageQuery.setDeviceType(DmcDeviceTypeEnum.CSUN_SLEEP.getIndex());
        csunSleepPageQuery.setOldmanId(oldmanId);

        PageHelper.startPage(1, 1);

        List<DmcCsunSleepVo> dmcCsunSleepVos = dmcCsunSleepBindMapper
                .queryGroupCsunSleepListWithPage(csunSleepPageQuery);

        if (PubUtils.isNull(dmcCsunSleepVos)) {
            return null;
        }

        return dmcCsunSleepVos.get(0);
    }

    /**
     * delete csun sleep by id
     *
     * @param id id
     * @return the int.
     */
    @Override
    public Integer deleteCsunSleepById(Long id) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunSleepByDeviceId(id);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610034, id);
        }

        // 2.判断智能床垫-日创是否已经绑定
        DmcCsunSleepBind dmcCsunSleepBindQuery = dmcCsunSleepBindMapper
                .selectByPrimaryKey(id);
        if (PublicUtil.isNotEmpty(dmcCsunSleepBindQuery)) {
            dmcCsunSleepBindMapper.deleteByPrimaryKey(id);
        }

        // 3.删除设备
        return dmcDeviceService.deleteDmcDeviceById(id);
    }

    @Override
    public Map<String, Object> getRecentData(Integer oldmanId) {
        return dmcCsunSleepBindMapper.getRecentData(oldmanId);
    }

    /**
     * query csun sleep data by ded oldman list
     *
     * @param bedOldmanSleepReqDtoList bed oldman sleep req dto list
     * @return the list
     */
    @Override
    public List<CsunSleepDataReqVo> queryCsunSleepDataByBedOldmanList(List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList) {

        return dmcCsunSleepBindMapper.queryCsunSleepDataByBedOldmanList(bedOldmanSleepReqDtoList);
    }

    /**
     * get oldman bed room building vo
     *
     * @param dmcCsunSleepVo dmc csun sleep vo
     * @return oldman bed room building vo
     */
    @Override
    public OldmanBedRoomBuildingVo getOldmanBedRoomBuildingVo(DmcCsunSleepVo dmcCsunSleepVo) {


        return null;
    }

    @Override
    public DmcDeviceByRoom selectRoomIdByDeviceId(Long alertId) {
        return dmcCsunSleepBindMapper.selectRoomIdByDeviceId(alertId);
    }

    @Override
    public List<SleepDataVo> selectCunuSleepData(SelectSleepDataDto selectSleepDataDto) {
        return dmcCsunSleepBindMapper.selectCunuSleepData(selectSleepDataDto);
    }

    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return dmcCsunSleepBindMapper.selectDeviceData(selectDeviceDataDto);
    }

    @Override
    public List<DmcCsunSleepVo> queryGroupCsunSleepList(CsunSleepVo csunSleepPageQuery) {

        List<DmcCsunSleepVo> dmcCsunSleepVoList = dmcCsunSleepBindMapper
                .queryGroupCsunSleepList(csunSleepPageQuery);
        return dmcCsunSleepVoList;
    }
}
